package com.drakulo.games.ais.ui;

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;

/**
 * This class ease font management
 * 
 * @author Drakulo
 * 
 */
public final class FontHelper {
	public static final String ARIAL = "data/fonts/arial.ttf";
	public static final String ASTROPOLIS = "data/fonts/astropolis.ttf";
	public static final String DARK_FUTURE = "data/fonts/DarkFutureItalic.ttf";
	public static final String ROBOTOR = "data/fonts/robotaur.ttf";
	public static final String WORLD_AT_WAR = "data/fonts/WorldsAtWarBB.ttf";
	public static final String VISITOR = "data/fonts/visitor1.ttf";
	public static final String ANONYMOUS = "data/fonts/Anonymous.ttf";
	public static final String DROID = "data/fonts/DroidSansMono.ttf";
	public static final String HEMI_HEAD = "data/fonts/HemiHead.ttf";
	public static final String FRAGILE_B = "data/fonts/FragileB.ttf";

	private static Map<String, UnicodeFont> cache = new HashMap<String, UnicodeFont>();

	private FontHelper() {
		// Nothing there
	}

	public static UnicodeFont getFont(String name) throws SlickException {
		return getFont(name, Color.black);
	}

	public static UnicodeFont getFont(String name, Color color)
			throws SlickException {
		return getFont(name, color, 16);
	}

	public static UnicodeFont getFont(String name, org.newdawn.slick.Color color, int size)throws SlickException{
		return getFont(name, convert(color), size);
	}
	
	public static UnicodeFont getFont(String name, Color color, int size)
			throws SlickException {
		String key = getKey(name, color, size);
		if (cache.get(key) == null) {
			loadFont(name, color, size);
		}
		return cache.get(key);
	}

	@SuppressWarnings("unchecked")
	private static void loadFont(String name, Color color, int size)
			throws SlickException {
		String key = getKey(name, color, size);
		UnicodeFont font = new UnicodeFont(name, size, false, false);
		font.addAsciiGlyphs();
		font.getEffects().add(new ColorEffect(color));
		font.loadGlyphs();
		cache.put(key, font);
	}

	public static UnicodeFont getDefaultFont() throws SlickException {
		return getFont(DROID);
	}

	public static UnicodeFont getDefaultFont(Color color) throws SlickException {
		return getFont(DROID, color);
	}

	private static String getKey(String name, Color color, int size) {
		return name + "_" + color.toString().toLowerCase() + "_" + size;
	}

	public static UnicodeFont getDefaultSmallFont() throws SlickException {
		return getFont(DROID, Color.BLACK, 10);
	}

	public static Color convert(org.newdawn.slick.Color c) {
		return new Color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
	}
	
	public static String firstToUpper(String s){
		return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
	}
}
